package cn.itsource.common.util;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class SMSUtils {
    @Value("${SMS.UID}")
    private String Uid;
    @Value("${SMS.KEY}")
    private String Key;

    public void sendMsg(String toPhone,String code){
        try {
            HttpClient client = new HttpClient();
            PostMethod post = new PostMethod("https://utf8api.smschinese.cn/");
            post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");//在头文件中设置转码
            NameValuePair[] data ={ new NameValuePair("Uid",Uid),
                    new NameValuePair("Key", Key),
                    new NameValuePair("smsMob",toPhone),
                    new NameValuePair("smsText","验证码："+code)};
            post.setRequestBody(data);
            client.executeMethod(post);
            post.releaseConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
